package com.qf.edu.controller;

import com.qf.edu.entity.Drugs;
import com.qf.edu.service.GetDrugsService;
import com.qf.edu.service.impl.GetDrugsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author:huayuzhuo
 * @email:2798866329@qq.com
 * @desc:药品库存查询控制
 * @datatime: 2022/5/30 22:21
 */
@WebServlet("/getDrugsKpCom")
public class GetDrugsKpCom extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        String id1 = req.getParameter("id");
        //字符串转Integer，如果是空，会报500字符串""的错
        Integer id2 = 0;
        if (!id1.equals("")){
            id2 = Integer.parseInt(id1);
        }

        String name = req.getParameter("name");
        System.out.println(id1);
        System.out.println(name);
        GetDrugsService drugsService = new GetDrugsServiceImpl();
        HttpSession session = req.getSession();
        Drugs drugs = new Drugs();
        drugs.setId(id2);
        drugs.setName(name);

        List<Drugs> drugsSer = drugsService.getDrugsSer(drugs);
        System.out.println(drugsSer);
        session.setAttribute("drugs", drugsSer);
        //路径必须jsp文件名前加文件夹路径，不然会404，也不能./，文件夹前面加不加/无所谓
        resp.sendRedirect("/nurse/getDrugsKp.jsp");

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
